/**Admin Presenter**/
// $codepro.audit.disable expressionValue, com.instantiations.assist.eclipse.analysis.unusedReturnValue
package edu.gatech.presenters;

import javax.swing.JOptionPane;

import edu.gatech.datastorage.PatientInformation;
import edu.gatech.gui.EditPatientPanel;
import edu.gatech.userclasses.Patient;
import edu.gatech.userclasses.SysAdmin;
import edu.gatech.userclasses.User;
import edu.gatech.userclasses.UserType;
import edu.gatech.utilities.Persistence;

/** Presenter for the admin GUI
 * 
 * @author Tyler Natoli
 * @version 1.0
 */
public class AdminPresenter {
	
	/**System admin performing actions**/
	private final SysAdmin currentAdmin;
	
	/**Patient value from radio button**/
	private static final int PATIENT = 0;
	
	/**Nurse value from radio button**/
	private static final int NURSE = 1;
	
	/**Doctor value from radio button**/
	private static final int DOCTOR = 2;
	
	/**Admin value from radio button**/
	private static final int ADMIN = 3;
	
	/**
	 * Constructor that uses the admin from the GUI
	 * @param currentAdmin
	 */
	public AdminPresenter(SysAdmin currentAdmin){
		this.currentAdmin = currentAdmin;
	}
	
	/**Creates a user based on what radio button is selected
	 * 
	 * @param username
	 * @param password
	 * @param radioButton
	 */
	public void createUser(String username, String password, int radioButton){
		boolean available = true;
		for (User u : Persistence.getUserList()){
			if(u.getUsername().equals(username)){
				available = false;
			}
		}
		if(available){
			if(radioButton == PATIENT){
				final PatientInformation info = new PatientInformation
				(username, password);
				currentAdmin.addPatient(info);
				JOptionPane.showMessageDialog(null, "Thank you for registering" +
						" with our system. " + "Your account has" + " been creeated!");
			}
			else if(radioButton == NURSE){
				currentAdmin.addUser(username, password, UserType.NURSE);
				JOptionPane.showMessageDialog(null, "Thank you for registering " +
						"with our " + "system. Your account has" + "been creeated!");
			}
			else if(radioButton == DOCTOR){
				currentAdmin.addUser(username, password, UserType.DOCTOR);
				JOptionPane.showMessageDialog(null, "Thank you for registering" +
						" with our system. " + "Your account has" + "been creeated!");
			}
			else if(radioButton == ADMIN){
				currentAdmin.addUser(username, password, UserType.SYSADMIN);
				JOptionPane.showMessageDialog(null, "Thank you for registering with" +
						" our system. " + "Your account has" + "been creeated!");
			}
			else{
				JOptionPane.showMessageDialog(null, "Please select your user type");
			}
			Persistence.save();
		}
		else{
			JOptionPane.showMessageDialog(null, "This username is already registered " +
					"in our system");
		}
	}
	
	/**
	 * Deletes a user based on a username
	 * @param username
	 */
	public void deleteUser(String username){
		User currentUser = null;
		for (User u : Persistence.getUserList()){
			if (username.equals(u.getName())){
				currentUser = u;
			}
		}
		if(currentUser != null){
			currentAdmin.deleteUser(currentUser);
			Persistence.save();
			JOptionPane.showMessageDialog(null, currentUser.getName() + 
					" has been deleted.");
		}
		else{
			JOptionPane.showMessageDialog(null, "User not found");
		}
	}
	
	/**
	 * Searches for a patient and brings the user to the
	 * edit patient pages
	 * @param username
	 */
	public void search(String username){
		final Patient patient = currentAdmin.searchForPatient(username);
		if(patient != null){
			final EditPatientPanel editPanel = new EditPatientPanel // $codepro.audit.disable variableUsage
			(patient, currentAdmin); 
		}
		else{
			JOptionPane.showMessageDialog(null, "Patient not found");
		}
	}
	
	/**Unsuspends an account
	 * 
	 * @param username
	 */
	public void unSuspend(String username){
		User currentUser = null;
		for (User u : Persistence.getUserList()){
			if (username.equals(u.getName())){
				currentUser = u;
			}
		}
		if(currentUser == null){
			JOptionPane.showMessageDialog(null, "User not found");
		}
		else{
			currentUser.setSuspended(false);
			JOptionPane.showMessageDialog(null, currentUser.getName() +
					" has been unsuspended");
			Persistence.save();
		}
	}
	
	/**Suspends an account
	 * 
	 * @param username
	 */
	public void suspend(String username){
		User currentUser = null;
		for (User u : Persistence.getUserList()){
			if (username.equals(u.getName())){
				currentUser = u;
			}
		}
		if(currentUser == null){
			JOptionPane.showMessageDialog(null, "User not found");
		}
		else{
			currentUser.setSuspended(true);
			JOptionPane.showMessageDialog(null, currentUser.getName() + 
					" has been suspended");
			Persistence.save();
		}
	}
	
	/**To string override
	 * @return String
	 */
	public String toString(){
		return "Admin Presenter";
	}
}
